package com.elhakimz.matra.geo;

import com.jme.math.Vector3f;
import com.jme.scene.Circle;
import com.jme.scene.Line;
import com.jme.scene.Node;
import com.jme.curve.BezierCurve;
import com.jme.curve.PolylineCurve;
import com.elhakimz.matra.util.GeoUtil;

import java.util.ArrayList;

/**
 * Contains shape vor visualizing sensor ranges
 */

public class GeoShape {

   public static Node Sector2D(Vector3f location, float startAngle,float radius,  int arc, float yradius){

       BezierCurve curve;
       Node node = new Node();

        ArrayList<Vector3f> vectors= new ArrayList();
        Vector3f[] avectors= new Vector3f[2];
        Vector3f[] bvectors= new Vector3f[2];


        float x=location.x;
        float y=location.z;

       System.out.println("open");
        avectors[0]=GeoUtil.V3f(x,y,0);

        if (yradius==0){
            yradius=radius;
        }

        double segAngle, theta, angle, angleMid, segs, ax, ay, bx=0, by=0, cx=0, cy=0;

        if (Math.abs(arc)>360) {
		   arc = 360;
	    }
       System.out.println("arc:"+String.valueOf(arc));

        segs = Math.ceil(Math.abs(arc)/45);

        System.out.println("segments:"+String.valueOf(segs));
        segAngle = arc/segs;
	// The math requires radians rather than degrees. To convert from degrees
	// use the formula (degrees/180)*Math.PI to get radians.
		theta = -(segAngle/180)*Math.PI;
	// convert angle startAngle to radians
	angle = -(startAngle/180)*Math.PI;
	// draw the curve in segments no larger than 45 degrees.
	  if (segs>0) {
		// draw a line from the center to the start of the curve
		ax = x+Math.cos(startAngle/180*Math.PI)*radius;
		ay = y+Math.sin(-startAngle/180*Math.PI)*yradius;
          avectors[1]=GeoUtil.V3f((float)ax,(float)ay,0f);
          vectors.add(GeoUtil.V3f((float)ax,(float)ay,0f));

		// Loop for drawing curve segments
		for (int i = 0; i<(int)segs; i++) {
			angle += theta;
			angleMid = angle-(theta/2);
			bx = x+Math.cos(angle)*radius;
			by = y+Math.sin(angle)*yradius;
			cx = x+Math.cos(angleMid)*(radius/Math.cos(theta/2));
			cy = y+Math.sin(angleMid)*(yradius/Math.cos(theta/2));

            vectors.add(GeoUtil.V3f((float)cx,(float)cy,0f));
            vectors.add(GeoUtil.V3f((float)bx,(float)by,0f));

    	}
		// close the wedge by drawing a line to the center
         bvectors[0]=GeoUtil.V3f((float)bx,(float)by,0f);
         bvectors[1]=GeoUtil.V3f(x,y,0f);
         System.out.println("closed");


	}
       Vector3f[] vs= new Vector3f[vectors.size()];
       //convert
       System.out.println(vectors.size());
       for(int i=0;i<vectors.size();i++){
           Vector3f v = vectors.get(i);
           vs[i] =v ;
           System.out.println(v);
       }

       curve = new BezierCurve("arc",vs);
       node.attachChild(curve);
       //lines
       PolylineCurve line1 = new PolylineCurve("line1",avectors);
       node.attachChild(line1);

       PolylineCurve line2 = new PolylineCurve("line2",bvectors);
       node.attachChild(line2);

       node.updateModelBound();

      return node;

    }

    public static Vector3f[] Arc2D(Vector3f location, float radius, float arc, float startAngle, float yRadius){

        return null;
    }

}


